<?php
namespace app\ausadmin\controller;
use app\ausadmin\controller\Common;
use think\Db;
use think\Controller;
use Godok\Org\Adminauth;
use think\Config;
use think\Cache;
use think\Session;
use think\Cookie;
use think\Request;
use think\Loader;
use think\View;

class Index extends Common
{
    /**
     * 控制台
     */
    
    public function index()
    {
        $groupids = \Godok\Org\Adminauth::user('groupids');
        $data = ['groupname'=>'游客'];
        if( !empty($groupids) ) {
            $group = Db::name( Config::get('auth.admin_group') )->where(['id' => $groupids[0]])->find();
            debug_file('-------index---------'.Db::getLastSql());
            if ( $group ) {
                $data['groupname'] = $group['title'];
            }
        }
        $data['menu'] = $this->menu2Html( \Godok\Org\Adminauth::menu("ismenu=1 AND status=1") );

		return $this->fetch('index',$data);
    }
    
    /**
     * 首页
     */
    public function home(){
        //$where = 'is_delete = 1';
        //当前运营商总数
        $where = 'is_delete = 1 and status = 1';
        $merchant = Db::name('operator')->where($where)->count();

        //查询当前总用户数(去掉重复的openid)
        $usertotal = Db::name('user')->where(['is_delete'=>1])->count('distinct(openid)');
        //当前设备在线数
        $online = Db::name('charecabinet')->where(['is_delete'=>1,'status'=>1])->select();
        $data = $online;
        $i = 0 ;//初始化变量
        $time = time(); 
        foreach ($data  as $k=>$v) {
            //echo $v['cabinet_id'].'<br/>';
            $data2 = Cache::store('redis')->get($v['cabinet_id']);//判断是否在线
            if(isset($data2['heartbeattime'])){
                $times = $time - $data2['heartbeattime'];
                if($times<=250){
                    /*$Send = controller('cloud/Send');//获取在线的充电宝数
                    $totals = $Send->StockNum($v['cabinet_id']);
                    $totals = $totals['info'];
                    $i += $totals;*/
                    $i++;
                }
            }
        }

        //查询总设备数
        $totalCabinet = Db::name('charecabinet')->where(['is_delete'=>1,'status'=>1])->count();
        //echo $i; die;
        //当月成交订单量
        $monthOrder = Db::name( 'operator_order')
            ->whereTime('create_time', 'month')
            ->where(['pay_status'=>1,'is_delete'=>1])
            ->count();

        //当周成交订单
        $account = Db::name( 'operator_order')
            ->whereTime('create_time', 'week')
            ->where(['pay_status'=>1,'is_delete'=>1])
            ->count();

        //运营商当月总订单
        $monthSumOrder = Db::name( 'rechargeorder')
               ->whereTime('create_time', 'month')
               ->where(['pay_status'=>1,'is_delete'=>1])
               ->count();    
        //本周新注册用户
        $weekuser =Db::name('user')->whereTime('create_time', 'week')->where(['is_delete'=>1])->count('distinct(openid)'); 
        //本周成交订单    
        $operator = Db::name('operator')
                  ->whereTime('create_time', 'week')
                  ->where('is_delete',1)
                  ->count();
        $this->assign('weekoperator',$operator);
        //当月运营商经营状态
        //$operator = Db::name('operator')->where(['is_delete'=>1,'status'=>1])->select();
        $time = gettimestamp('thismonth');
        $wl['is_delete'] = 1;
        $wl['status'] = 2;
        $wl['create_time'] = array(array('egt',$time['starttime']),array('elt',$time['endtime']), 'and');
        $monthRent = Db::name('leaseorder')
                      ->where($wl)
                      ->field("FROM_UNIXTIME(create_time,'%d') as unit,ifnull(sum(amount),0) as amount")
                      ->group("unit")
                      ->select();
        $data = array(
            'online' => $i,//在线设备
            'month'  => $monthOrder,
            'account'  => $account,//运营商开户
            'weekuser'  => $weekuser,
            'merchant'   => $merchant,
            'totalCabinet' => $totalCabinet,
            'usertotal'   => $usertotal,
            'monthSumOrder' => $monthSumOrder,
        ); 
        //var_dump($monthRent);die;
        $this->assign('monthRent',$monthRent);
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 格式化菜单
     */
    private  function menu2Html($list,$level = 0)
    {
        $menu = '';
        $menulevel = ['first','second','third'];
        if ( is_array($list) ) {
            if( $level > 2 ) {
                $level = 2;
            }
            $menu .= $level > 0  ? '<ul class="nav nav-'.$menulevel[$level].'-level">' : '';

            foreach ( $list as $li ) {
                $menu .= '<li>';
                if( empty($li['module']) || empty($li['controller']) || '*' == $li['module'] || '*' == $li['controller'] ) {
                    $menu .= '<a href="javascript:void(0)" >';
                } else {
                    $act = explode(',',$li['action']);
                    $href = \think\Request::instance()->root().'/'. $li['module'] . '/' . $li['controller'] . '/' . $act[0];
                    if( !empty($li['condition']) ) {
                        $href .= '?' . $li['condition'];
                    }
                    $menu .= '<a href="' . $href . '" class="J_menuItem">';
                }
    
                if ( !empty($li['icon']) ) {
                    $menu .= '<i class="' . $li['icon'] . '"></i> ';
                }
                $menu .= '<span class="nav-label">' . lang($li['title']) . '</span>';
                if ( isset($li['children']) ) {
                    $children = $this->menu2Html($li['children'], $level + 1 );
                    $menu .= ' <span class="fa arrow"></span>';
                } else {
                    $children = '';
                }
                $menu .= '</a>' . $children;
                $menu .= '</li>';
            }
    
            $menu .= $level > 0 ? '</ul>' : '';
            return $menu;
        } else {
            return '';
        }
    }


}
